package com.beyondsoft.sabg.edu.utils;

import cn.hutool.core.util.StrUtil;
import com.beyondsoft.sabg.edu.model.dto.BasicDTO;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

/**
 * @Author Mo cr
 * @Date 2022/11/11
 */
public class PageUtils {
    public static Pageable initPage(BasicDTO page) {
        int pageIndex = page.getCurrent();
        int pageSize = page.getPageSize();
        String sort = page.getSort();
        String strOrder = page.getOrder();
        Sort.Direction order = Sort.Direction.DESC;
        if (pageIndex < 1) {
            pageIndex = 1;
        }
        if (pageSize < 1) {
            pageSize = 10;
        }
        if (StrUtil.isEmpty(sort)) {
            sort = "updateTime";
        }
        if (StrUtil.isNotEmpty(strOrder) && strOrder.equalsIgnoreCase("ascend")) {
            order = Sort.Direction.ASC;
        }
        return PageRequest.of(pageIndex - 1, pageSize, order, sort);
    }
}

